package com.tjetc.view.teacherview;

import cn.hutool.core.date.DateTime;
import com.tjetc.proxy.CilentProxy;
import com.tjetc.view.studentview.MyCourseView_In_Content;
import com.zhiyun.domain.Course;
import com.zhiyun.domain.Teacher;
import com.zhiyun.service.CourseService;
import com.zhiyun.service.TeacherService;
import com.zhiyun.vo.AppData;
import com.zhiyun.vo.Result;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class MyCourseView {
    TeacherService teacherService=CilentProxy.getProxyInstance(TeacherService.class);
    CourseService courseService= CilentProxy.getProxyInstance(CourseService.class);
   Scanner scanner=new Scanner(System.in);
    public void start() {
        while (true){
            System.out.println("~~~~~~~~~~~我的课程界面~~~~~~~~~~~");
            System.out.println("1.课程列表");
            System.out.println("2.添加课程");
            System.out.println("3.进入课程");
            System.out.println("0.返回上一级");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    allCourse();
                    break;
                case 2:
                    inseretCourse();
                    break;
                case 3:
                    inCourse();

                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误,请重新输入");


            }
        }
    }

    private void inCourse() {
       while (true){
           Result result = courseService.teacherCourseList(AppData.teacher.getId());
           if (result.getCode()==200){
               boolean a=false;
               Course course1=new Course();
               List<Course> obj = (List<Course>) result.getObj();
               for (Course course : obj) {
                   Result result1 = teacherService.myInfo(course.getTeacherId());
                   if (result1.getCode()==200){
                       Teacher obj1 = (Teacher) result1.getObj();
                       System.out.println(course.getId()+"   课程名称:"+course.getName()+
                               (obj1.getTruename()==null?" ":("    老师姓名:"+obj1.getNickname()))+"   老师id:"+course.getTeacherId());
                   }
               }
               System.out.println("请输入想要进入的课程id");
               int i = scanner.nextInt();
               for (Course course : obj) {
                   if (i==course.getId()){
                       a=true;
                       course1=course;
                   }
               }
               if (a){
                   System.out.println("进入课程成功");
                   new MyCourseView_In().start(course1);
               }else {
                   System.out.println("要进入的课程不存在,请重新输入");
               }
           }else {
               System.out.println("课程加载失败");
           }

           break;
       }
    }

    private void inseretCourse() {
        while (true){
            System.out.println("请输入课程名称:");
            String name = scanner.next();
            System.out.println("请输入课程描述:");
            String describe = scanner.next();
            System.out.println("请输入开课时间:");
            System.out.println("年:");
            int i = scanner.nextInt();
            System.out.println("月:");
            int i1 = scanner.nextInt();
            System.out.println("日:");
            int i2 = scanner.nextInt();
            System.out.println("请输入结束时间:");
            DateTime dateTime = new DateTime(LocalDateTime.of(i, i1, i2, 0, 0, 0));
            System.out.println("年:");
            int j = scanner.nextInt();
            System.out.println("月:");
            int j1 = scanner.nextInt();
            System.out.println("日:");
            int j2 = scanner.nextInt();
            DateTime dateTime2 = new DateTime(LocalDateTime.of(j, j1, j2, 0, 0, 0));
            Result result = courseService.insertCourse(new Course(0, name, describe, dateTime, dateTime2, "", AppData.teacher.getId()));
            if (result.getCode()==200){
                System.out.println("创建成功");
                String obj = (String) result.getObj();
                System.out.println("邀请码是:"+obj);
            }else {
                System.out.println("创建失败~~~");
            }
            break;
        }
    }

    private void allCourse() {
      while (true){
          Result result = courseService.teacherCourseList(AppData.teacher.getId());
          if (result.getCode()==200){
              List<Course> obj = (List<Course>) result.getObj();
              for (Course course : obj) {
                  Result result1 = teacherService.myInfo(course.getTeacherId());
                  if (result1.getCode()==200){
                      Teacher obj1 = (Teacher) result1.getObj();
                      System.out.println(course.getId()+"   课程名称:"+course.getName()+
                              (obj1.getTruename()==null?" ":("    老师姓名:"+obj1.getNickname()))+"   老师id:"+course.getTeacherId());
                  }
              }
          }else {
              System.out.println("课程加载失败");
          }
          break;
      }
    }
}
